import { Component, Input } from '@angular/core';
import { CascaderItem } from 'ng-devui';
@Component({
  selector: 'app-edit-device-categroy',
  templateUrl: './edit-device-categroy.component.html',
  styleUrls: ['./edit-device-categroy.component.scss']
})
export class EditDeviceCategroyComponent {
  @Input() data: any;
  options :any=[];//部门
  EquiomentCategory={
    equipmentCategoryName: '',
    priority: null,
    parentEquipmentCategoryId: null,
  };//分类类目
  constructor(){}
  ngOnInit(): void{
    this.EquiomentCategory.equipmentCategoryName=this.data.option.equipmentCategoryName
    this.EquiomentCategory.priority=this.data.option.priority
    this.options = this.convertToCascaderItem(this.data.options)
    if(this.data.pathPids){
      this.EquiomentCategory.parentEquipmentCategoryId = this.data.pathPids[this.data.pathPids.length-1]
      this.disableItems(this.options, this.data.option.equipmentCategoryId);
    }
  }
  convertToCascaderItem(data: any[]): CascaderItem[] {
    return data.map(item => {
      let cascaderItem: CascaderItem = {
        label: item.equipmentCategoryName,
        value: item.equipmentCategoryId,
      };
      if (item.subEquipmentCategories) {
        cascaderItem.children = this.convertToCascaderItem(item.subEquipmentCategories);
      }
      return cascaderItem;
    });
  }//转换数据格式为华为库练级菜单的数据格式
  onChanges($event: string | any[]){
    this.EquiomentCategory.parentEquipmentCategoryId = $event[$event.length-1]
  }
   disableItems(items, targetValue) {
    for (let item of items) {
      if (item.value === targetValue) {
        item.disabled = true;
        this.disableAllChildren(item);
      } else if (item.children) {
        this.disableItems(item.children, targetValue);
      }
    }
  }
   disableAllChildren(item) {
    if (item.children) {
      for (let child of item.children) {
        child.disabled = true;
        this.disableAllChildren(child);
      }
    }
  }
}
